package algorithm.sort;

import java.util.Arrays;

/**
 * @ Description：选择排序
 */
public class _01SelectionSort extends BaseSorter {

    public static void main(String[] args) {
        Integer[] arr = SortUtils.getRandomIntArr(10, 100);
        System.out.println(Arrays.toString(arr));
        new _01SelectionSort().sort(arr);
        System.out.println(Arrays.toString(arr));
    }

    @Override
    public <T extends Comparable<T>> T[] sort(T[] unsorted) {
        int n = unsorted.length;
        for (int i = 0; i < n; i++) {
            int minIndex = i;
            for (int j = i + 1; j < n; j++) {
                minIndex = unsorted[minIndex].compareTo(unsorted[j]) < 0 ? minIndex : j;
            }
            if (minIndex != i) {
                SortUtils.swap(unsorted, i, minIndex);
            }
        }
        return unsorted;
    }

    @Override
    public BaseSorter getInstance() {
        return new _01SelectionSort();
    }
}
